package pri.hillchen.std.netty.echo;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

/**
 * Created by hillchen on 2018/3/31.
 */
public class EchoServerHandler extends ChannelHandlerAdapter {

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        String msgContent = (String) msg;
        if(EchoClientHandler.ORDER_CONNECT.equals(msg)){
            System.out.println("$:请输入！");
        }else{
            System.out.println("$:" + msgContent);
        }

        ByteBuf byteBuf = Unpooled.copiedBuffer((msgContent + EchoServer.ORDER_END).getBytes());
        ctx.writeAndFlush(byteBuf);
    }
}
